<?php 
class Application_Model_QualityProblem extends Application_Model_DomainObject {
    
    protected $_id;
    /**
     * 业务类型
     */
    protected $_objectType;
    /**
     * 交易Id
     */
    protected $_objectId;

    public static function addQualtiyProblem($objectType,$objectId)
    {
        $qualityItem = new Application_Model_QualityProblem();
        $qualityItem->objectType = $objectType;
        $qualityItem->ObjectId = $objectId;
        $qualityItem->save();
        return $qualityItem->Id;
    }
    
    public static function byObjectIdObjectType($objectId,$objectType)
    {
        return self::getDao()->queryOne(array(" objectId = "=>$objectId," and objectType = "=>$objectType));
    }
    
    public function getContent()
    {
        $contentItem =  Application_Model_QualityProblemContent::byProblemId($this->_id);
        if(!$contentItem){
            return false;
        }
        $contentArr = array();
        foreach ($contentItem as $contentVal) {
            $contentArr[$contentVal->Id]["content"] = $contentVal->Content;
            $contentArr[$contentVal->Id]["dateCreated"] = $contentVal->DateCreated;
            $contentArr[$contentVal->Id]["operator"] = $contentVal->operator;
            $contentArr[$contentVal->Id]["arrImg"] = $contentVal->getImg();
            $contentArr[$contentVal->Id]["status"] = $contentVal->Status;
            $contentArr[$contentVal->Id]["statusName"] = $contentVal->StatusName;
        }
        return $contentArr;
    }
    
    public function getContentTrack()
    {
        $contentItem =  Application_Model_QualityProblemContent::byProblemId($this->_id);
        if(!$contentItem){
            return false;
        }
        $contentArr = array();
        foreach ($contentItem as $contentVal) {
            $contentArr[$contentVal->Id]["content"] = $contentVal->Content;
            $contentArr[$contentVal->Id]["dateCreated"] = $contentVal->DateCreated;
            $contentArr[$contentVal->Id]["operator"] = $contentVal->operator;
            $contentArr[$contentVal->Id]["arrImg"] = $contentVal->getImg();
            $contentArr[$contentVal->Id]["status"] = $contentVal->Status;
            $contentArr[$contentVal->Id]["statusName"] = $contentVal->StatusName;
            $contentArr[$contentVal->Id]["arrTrack"] = $this->_qualityProblemTrack($contentVal->Id);
        }
        return $contentArr;
    }
    
    protected function _qualityProblemTrack($objectId)
    {
        $trackItem = Application_Model_Tracking::getAllByObjectTypeIdOperateType($this->_objectType, $objectId, Application_Model_Tracking::Type_Quality);
        $trackArr = array();
        foreach ($trackItem as $trackVal) {
            $trackArr[$trackVal->Id]["dateCreated"] = $trackVal->dateAdd;
            $trackArr[$trackVal->Id]["content"] = $trackVal->content;
            $trackArr[$trackVal->Id]["operator"] = $trackVal->operator;
        }
        return $trackArr;
    }

    public function getQualityProblemAll()
    {
        return Application_Model_QualityProblemContent::byProblemId($this->_id);
    }

    public function getNum($status)
    {
        return Application_Model_QualityProblemContent::getNum($this->_id,$status);
    }
    
    public function delProblem()
    {
        $item = $this->createdItem();
        if($this->_objectType != Application_Model_DomainobjectType::ObjectType_Order && $this->_objectType != Application_Model_DomainobjectType::ObjectType_OrderItem){
            $item->CkPass = 0;
            $item->save();
            return true;
        }
        if($this->_objectType == Application_Model_DomainobjectType::ObjectType_Order) {
            foreach ($item->getItems() as $itemVal) {
                $ifSolve = $this->solveItemAbnormal($itemVal);
                if(!$ifSolve){
                    return false;
                }
            }
            $item->isAbnormal = Application_Model_Order::Abnormal_Default;
            $item->CkPass = 0;
            $item->scheme = 0;
            $item->rechangeStatus = null;
            $item->save();
            return true;
        }
        if($this->_objectType == Application_Model_DomainobjectType::ObjectType_OrderItem) {
            $item->status = 0;
            $item->CkPass = 0;
            $item->save();
            $orderItem = Application_Model_Order::load($item->OrderId);
            foreach ($orderItem->getItems() as $itemVal) {
                $ifSolve = $this->solveItemAbnormal($itemVal);
                if(!$ifSolve){
                    return false;
                }
            }
            $orderItem->isAbnormal = Application_Model_Order::Abnormal_Default;
            $orderItem->CkPass = 0;
            $orderItem->scheme = 0;
            $orderItem->rechangeStatus = null;
            $orderItem->save();
            return true;
        }
    }
    
    protected function solveItemAbnormal($itemVal)
    {
        $ItemProblem = Application_Model_QualityProblem::byObjectIdObjectType($itemVal->Id, Application_Model_DomainobjectType::ObjectType_OrderItem);
        if($ItemProblem){
            foreach ($ItemProblem->getQualityProblemAll() as $qualityProbleVal) {
                if($qualityProbleVal->status == Application_Model_QualityProblemContent::Status_Default){
                    return false;
                }
            }
        }
        return true;
    }


    public function createdItem()
    {
        switch ($this->_objectType) {
            case Application_Model_DomainobjectType::ObjectType_Order:
                return Application_Model_Order::load($this->_objectId);
                break;
            case Application_Model_DomainobjectType::ObjectType_Ing:
                return Application_Model_ingstore::load($this->_objectId);
                break;
            case Application_Model_DomainobjectType::ObjectType_Exchange:
                return Application_Model_ReturnGoods::load($this->_objectId);
                break;
            case Application_Model_DomainobjectType::ObjectType_OrderItem:
                return Application_Model_OrderItem::load($this->_objectId);
                break;
            case Application_Model_DomainobjectType::ObjectType_Visiting:
                return Application_Model_VisitingServer::load($this->_objectId);
                break;
            case Application_Model_DomainobjectType::ObjectType_VisitingWay:
                return Application_Model_VisitingServer::load($this->_objectId);
                break;
            default:
                return null;
                break;
        }
    }
}